<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      element-loading-text="加载中"
      :data="list"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="50">
        <template slot-scope="scope">
          {{ scope.$index +1 }}
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="专业" align="center">
        <template slot-scope="scope">
          {{ scope.row.specialty }}
        </template>
      </el-table-column>
      <el-table-column label="获奖类型" align="center">
        <template slot-scope="scope">
          {{ scope.row.type }}
        </template>
      </el-table-column>
      <el-table-column label="年份" align="center">
        <template slot-scope="scope">
          {{ scope.row.year }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="220">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="studentEdit(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="studentDel(scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 40, 80]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 编辑 -->
    <studentEdit
      ref="edit"
      :form="detailFrom"
      @sonSay="fetchData"
    />
  </div>
</template>

<script>
import { getList, studentDel } from '@/api/student'
import studentEdit from './studentEdit.vue'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  components: { studentEdit },
  data() {
    return {
      list: null,
      listLoading: true,
      detailFrom: {},
      currentPage: 1,
      total: 9,
      pageSize: 10
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取表格数据
    fetchData() {
      // debugger
      // console.log('路由模式', process.env.port)
      this.listLoading = true
      const val = {
        page: (this.currentPage - 1), size: this.pageSize
      }
      getList(val).then((res) => {
        console.log(res)
        this.list = res.res.content
        this.total = res.res.totalElements
        this.currentPage = res.res.number + 1
        this.pageSize = res.res.size
        this.listLoading = false
      })
    },

    // 编辑单条广告
    studentEdit(data) {
      this.detailFrom = data
      this.$refs.edit.idDetail = true
    },
    // 删除单条广告
    studentDel(id) {
      studentDel(id)
        .then((res) => {
          console.log(res)
          this.$message.success('删除成功')
          this.fetchData()
        })
        .catch(() => {
          this.$message.error('删除失败')
        })
    },
    // 分页
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.currentPage = val
      this.fetchData()
    }
  }
}
</script>
